import { Request, Response } from "express";
import { NextFunction } from "express-serve-static-core";
const express = require("express");
const router = express.Router();

import * as config from "../../config";
import r from "../r";

// 临时密钥计算样例

const http = require("http");
const crypto = require("crypto");
const request = require("request");

router.post("/", async (req: any, res: Response, next: NextFunction) => {
  try {
    const newurl = `${config.api}/file/s3upload`;

    const ret = await r(
      {
        url: newurl,
        formData: {
          file: {
            value: req.files.file.data,
            options: {
              filename: req.files.file.name,
              contentType: req.files.file.mimetype
            }
          }
        }
      },
      req,
      res
    );

    const jsonRet = JSON.parse(ret.body);

    res.json({
      code: jsonRet.code,
      data: {
        id: jsonRet.data.id,
        status: jsonRet.data.status,
        url: jsonRet.data.s3path,
        name: jsonRet.data.filename
      },
      msg: jsonRet.msg
    });
  } catch (e) {
    res.json({
      code: -1,
      data: undefined,
      msg: e
    });
  }
});

export default router;
